package com.waypolice.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.waypolice.common.pojo.DecryptCommonResult;
import com.waypolice.common.utils.PageUtils;
import com.waypolice.common.utils.StringUtil;
import com.waypolice.manager.service.SecretArcStatisticsService;
import com.waypolice.manager.service.SinArcStatisticsService;
import com.waypolice.mapper.*;
import com.waypolice.mapper.handwriting.SecretArcStatisticsMapperSon;
import com.waypolice.mapper.handwriting.SinArcStatisticsMapperSon;
import com.waypolice.pojo.*;
import com.waypolice.pojo.handwriting.WriteSql;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by 312小时 on 2019/4/1.
 */
@Service
public class SecretArcStatisticsServiceImpl implements SecretArcStatisticsService {

    @Autowired
    private CmsJudItemMapper cmsJudItemMapper;
    @Autowired
    private CmsDecryptArchiveMapper cmsDecryptArchiveMapper;
    @Autowired
    private CmsDecryptMapper cmsDecryptMapper;
    @Autowired
    private CmsSinItemMapper cmsSinItemMapper;
    @Autowired
    private CmsJudVolumeMapper cmsJudVolumeMapper;
    @Autowired
    private SecretArcStatisticsMapperSon secretArcStatisticsMapperSon;
    @Autowired
    private UpmsDictionaryMapper upmsDictionaryMapper;

    @Override
    public Object getSecretArchiveDetailStatistics(int offset, int limit) {
        Map<String, Object> result = new HashMap<>();
        List<DecryptCommonResult> dcrlist = new ArrayList<>();
        CmsDecryptArchiveExample cmsDecryptArchive = new CmsDecryptArchiveExample();
        //-----------查询有密级的卷内件------------
        CmsJudItemExample cmsJudItemExample = new CmsJudItemExample();
        CmsJudItemExample.Criteria criteria = cmsJudItemExample.createCriteria();
     /*   criteria.andItemSecretNotEqualTo("");*/
        criteria.andItemSecretIsNotNull();
        List<CmsJudItem> items = cmsJudItemMapper.selectByExample(cmsJudItemExample);
        if (!items.isEmpty()) {
            //第一步：查出已归档的密级不为空或者不为公开的件，遍历
            for (CmsJudItem cmsJudItem : items) {
                Long itmeVolId = cmsJudItem.getItmeVolId();
                CmsJudVolume cmsJudVolume = cmsJudVolumeMapper.selectByPrimaryKey(itmeVolId);
                if (cmsJudVolume.getVolArchiveStatus() == 2) {
                    DecryptCommonResult decryptCommonResult = new DecryptCommonResult();
                    //档案id
                    decryptCommonResult.setArcid(cmsJudItem.getItemId().intValue());
                    //文号
                    if (!StringUtils.isBlank(cmsJudItem.getItemName())) {
                        decryptCommonResult.setItemName(cmsJudItem.getItemName());
                    } else {
                        decryptCommonResult.setItemName("");
                    }
                    //题名
                    if (!StringUtils.isBlank(cmsJudItem.getItemTitle())) {
                        decryptCommonResult.setItemTitle(cmsJudItem.getItemTitle());
                    } else {
                        decryptCommonResult.setItemTitle("");
                    }
                    //密级
                    if (!StringUtils.isBlank(cmsJudItem.getItemSecret())) {
                        String s = selectSecretByCode(cmsJudItem.getItemSecret());
                        decryptCommonResult.setItemSecret(s);
                    } else {
                        decryptCommonResult.setItemSecret("");
                    }
                    //保密期限
                    if (!StringUtils.isBlank(cmsJudItem.getItemStr6())) {
                        decryptCommonResult.setItemStorageSecret(cmsJudItem.getItemStr6());
                    } else {
                        decryptCommonResult.setItemStorageSecret("");
                    }
                    Long itemId = cmsJudItem.getItemId();
                    CmsDecryptArchiveExample.Criteria criteria1 = cmsDecryptArchive.createCriteria();
                    criteria1.andDrArcidEqualTo(itemId.intValue());
                    criteria1.andDrExtraoneEqualTo("1");
                    //关联，解密-档案中间表，解密表，查询卷内件解密信息
                    List<CmsDecryptArchive> cmsDecryptArchives = cmsDecryptArchiveMapper.selectByExample(cmsDecryptArchive);
                    if (!cmsDecryptArchives.isEmpty()) {
                        //关联表不为空
                        CmsDecryptArchive cmsDecryptArchive1 = cmsDecryptArchives.get(0);
                        CmsDecrypt cmsDecrypt = cmsDecryptMapper.selectByPrimaryKey(cmsDecryptArchive1.getDrDecrid());
                        //解密类型
                        if (!StringUtils.isBlank(cmsDecrypt.getDecrType())) {
                            decryptCommonResult.setDecrType(cmsDecrypt.getDecrType());
                        } else {
                            decryptCommonResult.setDecrType("");
                        }
                        //解密日期
                        if (!StringUtils.isBlank(cmsDecrypt.getDecrDate())) {
                            decryptCommonResult.setDecrDate(cmsDecrypt.getDecrDate());
                        } else {
                            decryptCommonResult.setDecrDate("");
                        }
                        //解密后处理
                        if (!StringUtils.isBlank(cmsDecrypt.getDecrResult())) {
                            decryptCommonResult.setDecrResult(cmsDecrypt.getDecrResult());
                        } else {
                            decryptCommonResult.setDecrResult("");
                        }
                        //定密责任人
                        if (!StringUtils.isBlank(cmsDecrypt.getDecrReperson())) {
                            decryptCommonResult.setDecrReperson(cmsDecrypt.getDecrReperson());
                        } else {
                            decryptCommonResult.setDecrReperson("");
                        }
                    } else {
                        //关联表为空
                        decryptCommonResult.setDecrType("");
                        decryptCommonResult.setDecrDate("");
                        decryptCommonResult.setDecrResult("");
                        decryptCommonResult.setDecrReperson("");
                    }
                    dcrlist.add(decryptCommonResult);
                }

            }
        }
        //-----------查询有密级的单件------------
        CmsSinItemExample cmsSinItemExample = new CmsSinItemExample();
        CmsSinItemExample.Criteria scriteria = cmsSinItemExample.createCriteria();
        scriteria.andItemArcStatusEqualTo(1L);
      /*  scriteria.andItemSecretNotEqualTo("");*/
        scriteria.andItemSecretIsNotNull();
        List<CmsSinItem> cmsSinItems = cmsSinItemMapper.selectByExample(cmsSinItemExample);
        if (!cmsSinItems.isEmpty()) {
            //第一步：查出已归档的密级不为空或者不为公开的件，遍历
            for (CmsSinItem cmsSinItem : cmsSinItems) {
                DecryptCommonResult decryptCommonResult1 = new DecryptCommonResult();
                //档案id
                decryptCommonResult1.setArcid(cmsSinItem.getItemId().intValue());
                //文号
                if (!StringUtils.isBlank(cmsSinItem.getItemName())) {
                    decryptCommonResult1.setItemName(cmsSinItem.getItemName());
                } else {
                    decryptCommonResult1.setItemName("");
                }
                //题名
                if (!StringUtils.isBlank(cmsSinItem.getItemTitle())) {
                    decryptCommonResult1.setItemTitle(cmsSinItem.getItemTitle());
                } else {
                    decryptCommonResult1.setItemTitle("");
                }
                //密级
                if (!StringUtils.isBlank(cmsSinItem.getItemSecret())) {
                    String s = selectSecretByCode(cmsSinItem.getItemSecret());
                    decryptCommonResult1.setItemSecret(s);
                } else {
                    decryptCommonResult1.setItemSecret("");
                }
                //保密期限
                if (!StringUtils.isBlank(cmsSinItem.getItemExp4())) {
                    decryptCommonResult1.setItemStorageSecret(cmsSinItem.getItemExp4());
                } else {
                    decryptCommonResult1.setItemStorageSecret("");
                }
                Long itemId = cmsSinItem.getItemId();
                CmsDecryptArchiveExample.Criteria criteria2 = cmsDecryptArchive.createCriteria();
                criteria2.andDrArcidEqualTo(itemId.intValue());
                criteria2.andDrExtraoneEqualTo("2");
                //关联，解密-档案中间表，解密表，查询卷内件解密信息
                List<CmsDecryptArchive> cmsDecryptArchives = cmsDecryptArchiveMapper.selectByExample(cmsDecryptArchive);
                if (!cmsDecryptArchives.isEmpty()) {
                    CmsDecryptArchive cmsDecryptArchive1 = cmsDecryptArchives.get(0);
                    CmsDecrypt cmsDecrypt = cmsDecryptMapper.selectByPrimaryKey(cmsDecryptArchive1.getDrDecrid());
                    //解密类型
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrType())) {
                        decryptCommonResult1.setDecrType(cmsDecrypt.getDecrType());
                    } else {
                        decryptCommonResult1.setDecrType("");
                    }
                    //解密日期
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrDate())) {
                        decryptCommonResult1.setDecrDate(cmsDecrypt.getDecrDate());
                    } else {
                        decryptCommonResult1.setDecrDate("");
                    }
                    //解密后处理
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrResult())) {
                        decryptCommonResult1.setDecrResult(cmsDecrypt.getDecrResult());
                    } else {
                        decryptCommonResult1.setDecrResult("");
                    }
                    //定密责任人
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrReperson())) {
                        decryptCommonResult1.setDecrReperson(cmsDecrypt.getDecrReperson());
                    } else {
                        decryptCommonResult1.setDecrReperson("");
                    }
                } else {
                    decryptCommonResult1.setDecrType("");
                    decryptCommonResult1.setDecrDate("");
                    decryptCommonResult1.setDecrResult("");
                    decryptCommonResult1.setDecrReperson("");
                }
                dcrlist.add(decryptCommonResult1);

            }
        }
        List<DecryptCommonResult> finalList = new ArrayList<DecryptCommonResult>();
        PageUtils<DecryptCommonResult> pageUtils = new PageUtils<DecryptCommonResult>();
        finalList = pageUtils.pageList(dcrlist, offset, limit);
        result.put("rows", finalList);
        result.put("total", dcrlist.size());
        return result;
    }

    @Override
    public Object getSecretPostArchiveDetailStatistics(int offset, int limit, String vol_archive_unit, String vol_archive_year, String vol_storage_time, String vol_secret, String starttime, String endtime) {
        Map<String, Object> result = new HashMap<>();
        List<DecryptCommonResult> dcrlist = new ArrayList<>();
        CmsDecryptArchiveExample cmsDecryptArchive = new CmsDecryptArchiveExample();
        //根据条件查询件
        String ext=" AND 1=1 ";
        if(vol_archive_unit!=null&&vol_archive_unit!=""){
            ext+=" AND v.VOL_ARCHIVE_UNIT LIKE '%"+vol_archive_unit+"%' ";
        }
        if(vol_archive_year!=null&&vol_archive_year!=""){
            ext+=" AND v.VOL_ARCHIVE_YEAR LIKE '%"+vol_archive_year+"%' ";
        }
        if(vol_storage_time!=null&&vol_storage_time!=""){
            ext+=" AND v.VOL_STORAGE_TIME LIKE '%"+vol_storage_time+"%' ";
        }
        if(vol_secret!=null&&vol_secret!=""){
            ext+=" AND C.ITEM_SECRET LIKE '%"+vol_secret+"%' ";
        }else{
            ext+=" AND C.ITEM_SECRET IS NOT NULL \n";
        }
        if(starttime==null||starttime==""){
            if(endtime!=null&&endtime!=""){
                ext+=" AND C.ITEM_STR11_DATE LIKE '%"+endtime+"%' ";
            }

        }else{
            if(endtime!=null&&endtime!=""){
                ext+=" AND C.ITEM_STR11_DATE >=  to_date('"+starttime+"','yyyy-MM-dd') AND C.ITEM_STR11_DATE <=to_date('"+endtime+"','yyyy-MM-dd') ";
            }else{
                ext+=" AND C.ITEM_STR11_DATE LIKE '%"+starttime+"%' ";
            }
        }
        String str = " SELECT COUNT (1) AS ITEMNUM,C.ITEM_NAME,C.ITEM_TITLE,C.ITEM_SECRET,C.ITEM_STR6,C.ITEM_ID,V.VOL_ARCHIVE_YEAR,V.VOL_ARCHIVE_UNIT,V.VOL_STORAGE_TIME,C.ITEM_STR11_DATE FROM CMS_JUD_ITEM C,CMS_JUD_VOLUME V " +
                "WHERE C.itme_vol_id = V.vol_id AND V.VOL_ARCHIVE_STATUS = 2" + ext +
                "GROUP BY C.ITEM_NAME,C.ITEM_TITLE,C.ITEM_SECRET,C.ITEM_STR6,C.ITEM_ID,V.VOL_ARCHIVE_YEAR,V.VOL_ARCHIVE_UNIT,V.VOL_STORAGE_TIME,C.ITEM_STR11_DATE";
        WriteSql sql = new WriteSql(str);
        List<Map<String, Object>> secretArcPageData = secretArcStatisticsMapperSon.getSecretArcPageData(sql);
        if(!secretArcPageData.isEmpty()){
            for (Map<String,Object> map:secretArcPageData) {
                DecryptCommonResult decryptCommonResult1 = new DecryptCommonResult();
            if(map.containsKey("ITEM_NAME")){
                decryptCommonResult1.setItemName(map.get("ITEM_NAME").toString());
            }else{
                decryptCommonResult1.setItemName("");
            }
            if(map.containsKey("ITEM_TITLE")){
                decryptCommonResult1.setItemTitle(map.get("ITEM_TITLE").toString());
            }else{
                decryptCommonResult1.setItemTitle("");
            }
            if(map.containsKey("ITEM_SECRET")){
                String s = selectSecretByCode(map.get("ITEM_SECRET").toString());
                decryptCommonResult1.setItemSecret(s);
            }else{
                decryptCommonResult1.setItemSecret("");
            }
            if(map.containsKey("ITEM_STR6")){
                decryptCommonResult1.setItemStorageSecret(map.get("ITEM_STR6").toString());
            }else{
                decryptCommonResult1.setItemStorageSecret("");
            }
            String item_id = map.get("ITEM_ID").toString();
                CmsDecryptArchiveExample.Criteria criteria2 = cmsDecryptArchive.createCriteria();
                criteria2.andDrArcidEqualTo(Integer.valueOf(item_id));
                criteria2.andDrExtraoneEqualTo("1");
                //关联，解密-档案中间表，解密表，查询卷内件解密信息
                List<CmsDecryptArchive> cmsDecryptArchives = cmsDecryptArchiveMapper.selectByExample(cmsDecryptArchive);
                if (!cmsDecryptArchives.isEmpty()) {
                    CmsDecryptArchive cmsDecryptArchive1 = cmsDecryptArchives.get(0);
                    CmsDecrypt cmsDecrypt = cmsDecryptMapper.selectByPrimaryKey(cmsDecryptArchive1.getDrDecrid());
                    //解密类型
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrType())) {
                        decryptCommonResult1.setDecrType(cmsDecrypt.getDecrType());
                    } else {
                        decryptCommonResult1.setDecrType("");
                    }
                    //解密日期
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrDate())) {
                        decryptCommonResult1.setDecrDate(cmsDecrypt.getDecrDate());
                    } else {
                        decryptCommonResult1.setDecrDate("");
                    }
                    //解密后处理
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrResult())) {
                        decryptCommonResult1.setDecrResult(cmsDecrypt.getDecrResult());
                    } else {
                        decryptCommonResult1.setDecrResult("");
                    }
                    //定密责任人
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrReperson())) {
                        decryptCommonResult1.setDecrReperson(cmsDecrypt.getDecrReperson());
                    } else {
                        decryptCommonResult1.setDecrReperson("");
                    }
                } else {
                    decryptCommonResult1.setDecrType("");
                    decryptCommonResult1.setDecrDate("");
                    decryptCommonResult1.setDecrResult("");
                    decryptCommonResult1.setDecrReperson("");
                }
                dcrlist.add(decryptCommonResult1);
            }

        }
        //-------------查询单件-------------//
        String sext=" AND 1=1 ";
        if(vol_archive_unit!=null&&vol_archive_unit!=""){
            sext+=" AND ITEM_EXP2 LIKE '%"+vol_archive_unit+"%' ";
        }
        if(vol_archive_year!=null&&vol_archive_year!=""){
            sext+=" AND ITEM_EXP3 LIKE '%"+vol_archive_year+"%' ";
        }
        if(vol_storage_time!=null&&vol_storage_time!=""){
            sext+=" AND ITEM_NUM LIKE '%"+vol_storage_time+"%' ";
        }
        if(vol_secret!=null&&vol_secret!=""){
            sext+=" AND ITEM_SECRET LIKE '%"+vol_secret+"%' ";
        }else{
            sext+=" AND ITEM_SECRET IS NOT NULL\n";
        }
        if(starttime==null||starttime==""){
            if(endtime!=null&&endtime!=""){
                sext+=" AND ITEM_STR1_DATE LIKE '%"+endtime+"%' ";
            }
        }else{
            if(endtime!=null&&endtime!=""){
                sext+=" AND ITEM_STR1_DATE >=  to_date('"+starttime+"','yyyy-MM-dd') AND ITEM_STR1_DATE <=to_date('"+endtime+"','yyyy-MM-dd') ";
            }else{
                sext+=" AND ITEM_STR1_DATE LIKE '%"+starttime+"%' ";
            }
        }
        String sstr="SELECT COUNT (1) AS SINITEM,ITEM_ID,ITEM_NAME,ITEM_TITLE,ITEM_SECRET,ITEM_EXP4,ITEM_STR1_DATE,ITEM_EXP3,ITEM_EXP2,ITEM_NUM FROM CMS_SIN_ITEM WHERE ITEM_ARC_STATUS = '1'" + sext + "GROUP BY ITEM_ID,ITEM_NAME,ITEM_TITLE,ITEM_SECRET,ITEM_EXP4,ITEM_STR1_DATE,ITEM_EXP3,ITEM_EXP2,ITEM_NUM";
        WriteSql ssql = new WriteSql(sstr);
        List<Map<String, Object>> secretSinArcPageData = secretArcStatisticsMapperSon.getSecretArcPageData(ssql);
        if(!secretSinArcPageData.isEmpty()) {
            for (Map<String,Object> map:secretSinArcPageData) {
                DecryptCommonResult decryptCommonResult = new DecryptCommonResult();
                if(map.containsKey("ITEM_NAME")){
                    decryptCommonResult.setItemName(map.get("ITEM_NAME").toString());
                }else{
                    decryptCommonResult.setItemName("");
                }
                if(map.containsKey("ITEM_TITLE")){
                    decryptCommonResult.setItemTitle(map.get("ITEM_TITLE").toString());
                }else{
                    decryptCommonResult.setItemTitle("");
                }
                if(map.containsKey("ITEM_SECRET")){
                    String s = selectSecretByCode(map.get("ITEM_SECRET").toString());
                    decryptCommonResult.setItemSecret(s);
                }else{
                    decryptCommonResult.setItemSecret("");
                }
                if(map.containsKey("ITEM_EXP4")){
                    decryptCommonResult.setItemStorageSecret(map.get("ITEM_EXP4").toString());
                }else{
                    decryptCommonResult.setItemStorageSecret("");
                }
                String item_id = map.get("ITEM_ID").toString();
                CmsDecryptArchiveExample.Criteria criteria2 = cmsDecryptArchive.createCriteria();
                criteria2.andDrArcidEqualTo(Integer.valueOf(item_id));
                criteria2.andDrExtraoneEqualTo("2");
                //关联，解密-档案中间表，解密表，查询卷内件解密信息
                List<CmsDecryptArchive> cmsDecryptArchives = cmsDecryptArchiveMapper.selectByExample(cmsDecryptArchive);
                if (!cmsDecryptArchives.isEmpty()) {
                    CmsDecryptArchive cmsDecryptArchive1 = cmsDecryptArchives.get(0);
                    CmsDecrypt cmsDecrypt = cmsDecryptMapper.selectByPrimaryKey(cmsDecryptArchive1.getDrDecrid());
                    //解密类型
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrType())) {
                        decryptCommonResult.setDecrType(cmsDecrypt.getDecrType());
                    } else {
                        decryptCommonResult.setDecrType("");
                    }
                    //解密日期
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrDate())) {
                        decryptCommonResult.setDecrDate(cmsDecrypt.getDecrDate());
                    } else {
                        decryptCommonResult.setDecrDate("");
                    }
                    //解密后处理
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrResult())) {
                        decryptCommonResult.setDecrResult(cmsDecrypt.getDecrResult());
                    } else {
                        decryptCommonResult.setDecrResult("");
                    }
                    //定密责任人
                    if (!StringUtils.isBlank(cmsDecrypt.getDecrReperson())) {
                        decryptCommonResult.setDecrReperson(cmsDecrypt.getDecrReperson());
                    } else {
                        decryptCommonResult.setDecrReperson("");
                    }
                } else {
                    decryptCommonResult.setDecrType("");
                    decryptCommonResult.setDecrDate("");
                    decryptCommonResult.setDecrResult("");
                    decryptCommonResult.setDecrReperson("");
                }
                dcrlist.add(decryptCommonResult);
            }
        }
        List<DecryptCommonResult> finalList = new ArrayList<DecryptCommonResult>();
        PageUtils<DecryptCommonResult> pageUtils = new PageUtils<DecryptCommonResult>();
        finalList = pageUtils.pageList(dcrlist, offset, limit);
        result.put("rows", finalList);
        result.put("total", dcrlist.size());
        return result;
    }

    @Override
    public String selectSecretByCode(String itemSecretCode) {
        UpmsDictionaryExample upmsDictionaryExample = new UpmsDictionaryExample();
        UpmsDictionaryExample.Criteria criteria = upmsDictionaryExample.createCriteria();
        criteria.andCodeEqualTo(itemSecretCode);
        List<UpmsDictionary> upmsDictionaries = upmsDictionaryMapper.selectByExample(upmsDictionaryExample);
        if(upmsDictionaries.isEmpty()){
            return "";
        }
        return upmsDictionaries.get(0).getDescription();
    }
}
